package util.lang.sql;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class SqlOrderByStatementSet extends SqlOrderByStatement {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<SqlSingleOrderByStatement> listOfOrders;
	
	public SqlOrderByStatementSet() {
		listOfOrders = new LinkedList<SqlSingleOrderByStatement>();
	}
	
	public SqlOrderByStatementSet(SqlSingleOrderByStatement item) {
		this();
		listOfOrders.add(item);
	}
	public SqlOrderByStatementSet(Iterable<SqlSingleOrderByStatement> list) {
		this();
		add(list);
	}
	
	public void add(SqlSingleOrderByStatement item) {
		listOfOrders.add(item);
	}
	
	public void add(Iterable<SqlSingleOrderByStatement> list) {
        for (Iterator<SqlSingleOrderByStatement> i = list.iterator(); i.hasNext();) { 
        	SqlSingleOrderByStatement eachItem = (SqlSingleOrderByStatement) i.next();
        	listOfOrders.add(eachItem);
        }
	}
	
	@Override
	public String getSql() {

		StringBuffer sb = new StringBuffer();
		String con = " , ";
		
        for (Iterator<SqlSingleOrderByStatement> i = listOfOrders.iterator(); i.hasNext();) { 
        	SqlSingleOrderByStatement eachItem = (SqlSingleOrderByStatement) i.next();
        	sb.append(eachItem.getSqlNoOrderBy());
        	sb.append(con);
        }
        
        if (sb.length() == 0)
        	return "";
        
        return " ORDER BY " + sb.substring(0, sb.length() - con.length());
	}

	@Override
	public LinkedList<Object> getValues() {
		LinkedList<Object> objs = new LinkedList<Object>();
		
        for (Iterator<SqlSingleOrderByStatement> i = listOfOrders.iterator(); i.hasNext();) { 
        	SqlSingleOrderByStatement eachItem = (SqlSingleOrderByStatement) i.next();
        	LinkedList<Object> values = eachItem.getValues();
        	if (values != null) objs.addAll(values);
        }
        
        return objs;
	}

}
